package com.yztc.ui.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.bean.home.ListLcBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.classifydetail.ClassifyDetailActivity;
import com.yztc.ui.details.DetailsActivity;
import com.yztc.ui.WebViewActivity;
import com.yztc.utils.ImageHelper;
import com.yztc.utils.MyGirdView;

import java.util.List;

/**
 * Created by My on 2016/10/8.
 */
public class HomeBaseAdapter extends BaseAdapter {
    private Context mContext;
    private List<ListLcBean> mListLcBean;
    public HomeBaseAdapter(Context mContext,List<ListLcBean> mListLcBean){
        this.mListLcBean = mListLcBean;
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        return mListLcBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mListLcBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    ViewHodle hodle = null;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //为了和下面作区分
        final int bigPosition = position;
        if (convertView == null){
           convertView = LayoutInflater.from(mContext).inflate
                   (R.layout.fragment_home_listview_item,null);
            hodle = new ViewHodle();
            hodle.tvFloorName = (TextView) convertView.findViewById(R.id.tv_floor_name);
            hodle.tvFloorTitle = (TextView) convertView.findViewById(R.id.tv_floor_title);
            hodle.ivTitlePic = (ImageView) convertView.findViewById(R.id.iv_m_title_pic);
            hodle.gvHome = (MyGirdView) convertView.findViewById(R.id.gv_home);
            convertView.setTag(hodle);
        }else{
            hodle = (ViewHodle) convertView.getTag();
        }
        ImageHelper.getNormalImage(mContext,
                mListLcBean.get(position).getBanner1().getPic(),hodle.ivTitlePic);
        //为图片设置监听,点击跳转的相应的网页
            hodle.ivTitlePic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("link",mListLcBean.get(position).getBanner1().getBanner1Url());
                    bundle.putString("title","");
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });

        hodle.tvFloorName.setText(mListLcBean.get(position).getBasic().getFloorName());
        hodle.tvFloorTitle.setText(mListLcBean.get(position).getBasic().getFloorTitle());

        HomeGVBaseAdapter adapter = new HomeGVBaseAdapter(mListLcBean.get(position).getList(),
                mContext,mListLcBean.get(position).getBasic());
        hodle.gvHome.setAdapter(adapter);
        //为MyGirdView设置监听，点击item跳转到相应的产品详情
        hodle.gvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //分情况，最后一个item之前的item点击跳转的是产品详情，最后一个跳转的是更多产品
                String chunboPrice = mListLcBean.get(bigPosition).getList().get(position).getChunboPrice();
                String discountPrice = mListLcBean.get(bigPosition).getList().get(position).getDiscountPrice();
                if (position < mListLcBean.get(bigPosition).getList().size() - 1) {
                    //判断产品是否打折，如打折，则把打折价格传递到详情页面
                    if (!"0.00".equals(discountPrice)){
                         Intent intent = new Intent(mContext,DetailsActivity.class);
                         Bundle bundle = new Bundle();
                         bundle.putString("product_id", mListLcBean.get(bigPosition).getList().get(position).getProductId() + "");
                         bundle.putString("discount_price",discountPrice);
                         intent.putExtras(bundle);
                         mContext.startActivity(intent);
                  }else {
                        Intent intent = new Intent(mContext,DetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("product_id", mListLcBean.get(bigPosition).getList().get(position).getProductId() + "");
                        bundle.putString("discount_price",null);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                }else if (position == mListLcBean.get(bigPosition).getList().size() - 1){
                    Intent intent = new Intent(mContext,ClassifyDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("category_id", mListLcBean.get(bigPosition).getParentId());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    static class ViewHodle{
        TextView tvFloorName,tvFloorTitle;
        ImageView ivTitlePic;
        MyGirdView gvHome;
    }
}
